package com.wwwzh.himalayafm.fragment.disfragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.activity.BroadTwoActivity;
import com.wwwzh.himalayafm.activity.LiveTwoActivity;
import com.wwwzh.himalayafm.adapter.live_adapter.GridAdapter;
import com.wwwzh.himalayafm.adapter.live_adapter.LocalAdapter;
import com.wwwzh.himalayafm.adapter.live_adapter.TopAdapter;
import com.wwwzh.himalayafm.callback.IsUpListener;
import com.wwwzh.himalayafm.entity.entity_live.Categories;
import com.wwwzh.himalayafm.entity.entity_live.LiveTatolEntity;
import com.wwwzh.himalayafm.entity.entity_live.TopRadios;
import com.wwwzh.himalayafm.retrofit.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 * 广播界面
 */
public class LiveFragment extends Fragment implements Callback<LiveTatolEntity>, View.OnClickListener, IsUpListener {


    private RecyclerView recyclerTop;
    private RecyclerView recyclerlocal;
    private TopAdapter adapter;
    private LocalAdapter adapter1;
    private GridView grid;
    private GridAdapter adapter2;
    //按钮收起
    private List<Categories> list;
    private List<Categories> data;
    private List<Categories> datas;
    private List<TopRadios> top;

    public LiveFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_live, container, false);
        initView(rootView);
        initData();
        return rootView;

    }

    private void initData() {
        HttpService.LiveHttpService service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.LiveHttpService.class);
        service.getData().enqueue(this);
        recyclerlocal.setAdapter(adapter1);
        recyclerTop.setAdapter(adapter);
    }

    private void initView(View rootView) {
        data = new ArrayList<>();
        recyclerTop = (RecyclerView) rootView.findViewById(R.id.live_top_recycle);
        recyclerlocal = (RecyclerView) rootView.findViewById(R.id.live_local_recycleView);
        TextView tv_local = (TextView) rootView.findViewById(R.id.live_local_btn);
        TextView tv_count = (TextView) rootView.findViewById(R.id.live_cou_btn);
        TextView tv_city = (TextView) rootView.findViewById(R.id.live_pro_btn);
        TextView tv_net = (TextView) rootView.findViewById(R.id.live_net_btn);
        tv_local.setOnClickListener(this);
        tv_count.setOnClickListener(this);
        tv_city.setOnClickListener(this);
        tv_net.setOnClickListener(this);

        grid = (GridView) rootView.findViewById(R.id.My_GridView);
        adapter = new TopAdapter(getContext());
        adapter1 = new LocalAdapter(getContext());

    }

    @Override
    public void onResponse(Call<LiveTatolEntity> call, Response<LiveTatolEntity> response) {
        LiveTatolEntity body = response.body();
        adapter1.addAll(body.getData().getLocalRadios());
        top = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            top.add(body.getData().getTopRadios().get(i));
        }
        adapter.addAll(top);
        data = response.body().getData().getCategories();
        list = new ArrayList<>();
            for (int i = 0; i < 8; i++) {
                list.add(body.getData().getCategories().get(i));
            }
        adapter2 = new GridAdapter(getContext(), list,data, this);
        grid.setAdapter(adapter2);

    }

    @Override
    public void onFailure(Call<LiveTatolEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();

    }


    @Override
    public void onClick(View v) {
        String str = "";
        switch (v.getId()) {
            case R.id.live_local_btn:
                str = "本地台";
                Intent intent = new Intent(getContext(), BroadTwoActivity.class);
                intent.putExtra("text", str);
                startActivity(intent);
                break;
            case R.id.live_cou_btn:
                str = "国家台";
                Intent intent2 = new Intent(getContext(), BroadTwoActivity.class);
                intent2.putExtra("text", str);
                startActivity(intent2);
                break;
            case R.id.live_pro_btn:
                str = "省市台";
                Intent intent4 = new Intent(getContext(), LiveTwoActivity.class);
                intent4.putExtra("text", str);
                startActivity(intent4);
                break;
            case R.id.live_net_btn:
                str = "网络台";
                Intent intent3 = new Intent(getContext(), BroadTwoActivity.class);
                intent3.putExtra("text", str);
                startActivity(intent3);
                break;
        }
    }
    @Override
    public void isUp(List<Categories> catelist) {
        datas = new ArrayList<>();
        datas.addAll(catelist);
        adapter2 = new GridAdapter(getContext(), datas,data, this);
        grid.setAdapter(adapter2);
    }
}
